﻿using System;
using System.Collections.Specialized;
using System.Web.UI.WebControls;
using ltModel;
using Telerik.Web.UI;

public partial class ltMan_users_userList : AdminBasePage
{
    ltModel.BLL.article_user users = new ltModel.BLL.article_user();
    ltModel.BLL.userTeam uteam = new ltModel.BLL.userTeam();

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlPager pager = new SqlPager("lt_article_user", "user_id", "*");
        if (!string.IsNullOrEmpty(sName.Text))
            pager.Where = " username like '%" + sName.Text.Replace("'", "''") + "%'";
        pager.OrderBy = "regdate desc";
        RadGridBinder.Create(RadGrid1, pager);
    }
    protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
    {
        int uid = int.Parse(GridCommander.ExtractValue(e, "user_id"));
        users.Delete(uid);
        uteam.Delete(uid);
    }
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "Lock")
        {
            string ids = GridCommander.JoinValues(e.Item.OwnerTableView.DataKeyValues, RadGrid1.SelectedIndexes);
            OperateLog.AddSuccessLog(CurrentUser.user_id, Request.UserHostAddress, "用户管理-用户锁定>" + ids);
            users.LockEdit(ids, e.CommandArgument.ToString());
            RadGrid1.Rebind();
        }
    }
    protected void b1_Click(object sender, EventArgs e)
    {
        RadGrid1.Rebind();
    }
    protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        HybridDictionary dict = GridCommander.ExtractValues(e);
        ltModel.BLL.article_user abl = new ltModel.BLL.article_user();
        if (abl.GetList("username='" + DataProtector.ToSBC(dict["username"].ToString()) + "' and user_id <> " + dict["user_id"].ToString()).Tables[0].Rows.Count > 0)
        {
            AddScriptM("radalert('该用户名已经存在！', 280, 120);");
            OperateLog.AddFailedLog(CurrentUser.user_id, Request.UserHostAddress, "用户管理-修改用户>用户名[" + dict["username"].ToString() + "]已经存在");
            e.Canceled = true;
            return;
        }
        ltModel.Model.article_user abo = abl.GetModel(Convert.ToInt32(dict["user_id"]));
        abo.username = DataProtector.ToSBC(dict["username"].ToString());
        if (dict["userpass"].ToString().Trim() != "")
            abo.userpass = DataProtector.EncryptDBString(dict["userpass"].ToString().Trim());
        abo.email = DataProtector.ToSBC(dict["email"].ToString().Trim());
        abo.homepage = DataProtector.ToSBC(dict["homepage"].ToString().Trim());
        abo.is_man = "1";
        abo.jm = DataProtector.ToSBC(dict["jm"].ToString().Trim());
        abo.oicq = dict["oicq"].ToString().Trim();
        abo.sex = Str.IsInt(dict["sex"].ToString()) ? dict["sex"].ToString() : Convert.ToBoolean(dict["sex"].ToString()) ? "1" : "0";
        abo.truename = DataProtector.ToSBC(dict["truename"].ToString().Trim());
        abl.Update(abo);
        DataCaches.SetUser(abo.user_id, abo);
        OperateLog.AddSuccessLog(CurrentUser.user_id, Request.UserHostAddress, "用户管理-修改用户>" + abo.username);
    }
    protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
    {
        HybridDictionary dict = GridCommander.ExtractValues(e);
        ltModel.Model.article_user abo = new ltModel.Model.article_user();
        ltModel.BLL.article_user abl = new ltModel.BLL.article_user();
        if (abl.GetList("username='" + DataProtector.ToSBC(dict["username"].ToString()) + "'").Tables[0].Rows.Count > 0)
        {
            AddScriptM("radalert('该用户名已经存在！', 280, 120);");
            OperateLog.AddFailedLog(CurrentUser.user_id, Request.UserHostAddress, "用户管理-添加用户>用户名[" + dict["username"].ToString() + "]已经存在");
            e.Canceled = true;
            return;
        }
        abo.username = DataProtector.ToSBC(dict["username"].ToString());
        abo.userpass = DataProtector.EncryptDBString(dict["userpass"].ToString());
        abo.email = DataProtector.ToSBC(dict["email"].ToString());
        abo.fb_num = 0;
        abo.funcqx = string.Empty;
        abo.homepage = DataProtector.ToSBC(dict["homepage"].ToString());
        abo.is_man = "1";
        abo.jm = DataProtector.ToSBC(dict["jm"].ToString());
        abo.lasttime = DateTime.Now.ToString();
        abo.lmqx = string.Empty;
        abo.Lock = "0";
        abo.oicq = dict["oicq"].ToString();
        abo.regdate = DateTime.Now.ToString();
        abo.regip = Request.UserHostAddress;
        abo.role_id = 1;
        abo.sex = dict["sex"].ToString();
        abo.sh_num = 0;
        abo.truename = DataProtector.ToSBC(dict["truename"].ToString());
        abo.user_id = abl.Add(abo);
        OperateLog.AddSuccessLog(CurrentUser.user_id, Request.UserHostAddress, "用户管理-添加用户>" + abo.username);

        ltModel.Model.userTeam ut = new ltModel.Model.userTeam();
        ut.user_id = abo.user_id;
        ut.sh = ut.tj = ut.add = ut.del = ut.info = "1";
        ut.funcqx = ut.lmqx = "0";
        uteam.Add(ut);
    }
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
        {
            RadButton sex = e.Item.FindControl("cSex") as RadButton;
            HiddenField hidden2 = e.Item.FindControl("HiddenField2") as HiddenField;
            sex.SelectedToggleStateIndex = string.IsNullOrEmpty(hidden2.Value) || !Convert.ToBoolean(hidden2.Value) ? 0 : 1;
            sex.OnClientToggleStateChanged = "function(s,e){$('#" + hidden2.ClientID + "').val(s.get_selectedToggleStateIndex());}";
            if (string.IsNullOrEmpty(hidden2.Value))
                hidden2.Value = sex.SelectedToggleStateIndex.ToString();
        }
    }
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
        {
            RadButton sex = e.Item.FindControl("cSex") as RadButton;
            HiddenField hidden2 = e.Item.FindControl("HiddenField2") as HiddenField;
            sex.SelectedToggleStateIndex = string.IsNullOrEmpty(hidden2.Value) || !Convert.ToBoolean(hidden2.Value) ? 0 : 1;
            if (string.IsNullOrEmpty(hidden2.Value))
                hidden2.Value = sex.SelectedToggleStateIndex.ToString();
        }
    }
}